package thread;

public class DeadLockDemo {
    private static Object chopsticks = new Object();//筷子
    private static Object spoon = new Object();//勺
    public static void main(String[] args) {
        Thread t1 = new Thread() {
            public void run() {
                System.out.println("北方人开始吃饭");
                System.out.println("北方人去拿筷子");
                synchronized (chopsticks) {
                    try {
                        System.out.println("北方人拿起筷子吃饭");
                        Thread.sleep(5000);
                        System.out.println("北方人吃完了");
                        System.out.println("北方人去拿勺子");
                    } catch (InterruptedException e) {}
                }
                System.out.println("北方人放下了筷子，去拿勺子");
                synchronized (spoon) {
                    try {
                        System.out.println("北方人拿到勺子开始喝汤");
                        Thread.sleep(5000);
                        System.out.println("北方人喝完汤");
                        System.out.println("北方人吃完了");
                    } catch (InterruptedException e) {
                    }
                }
            }
        };
        Thread t2 = new Thread() {
            public void run() {
                System.out.println("南方人开始吃饭");
                System.out.println("南方人去拿勺子");
                synchronized (spoon) {
                    try {
                        System.out.println("南方人拿起勺子喝汤");
                        Thread.sleep(5000);
                        System.out.println("南方人吃完了");
                        System.out.println("南方人去拿筷子");
                    }catch (InterruptedException e) {}
                    }
                System.out.println("南方人放下了勺子，去拿筷子");
                        synchronized (chopsticks) {
                    try {
                            System.out.println("南方人拿到筷子开始吃饭");
                            Thread.sleep(5000);
                            System.out.println("南方人吃完饭");
                            System.out.println("南方人吃完了");
                        }
                     catch (InterruptedException e) {}
                        }
            }
        };
        t1.start();
        t2.start();
    }
}
